package com.ma.admin.model

import com.ma.admin.handler.TripsDataProxy
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.hibernate.annotations.SQLDelete
import xyz.erupt.annotation.Erupt
import xyz.erupt.annotation.EruptField
import xyz.erupt.annotation.sub_erupt.Filter
import xyz.erupt.annotation.sub_field.Edit
import xyz.erupt.annotation.sub_field.EditType
import xyz.erupt.annotation.sub_field.View
import xyz.erupt.annotation.sub_field.sub_edit.*
import xyz.erupt.upms.handler.SqlChoiceFetchHandler
import java.io.Serializable
import java.util.*
import javax.persistence.*

/**
 * 车次管理
 *
 * @author yong
 * @date 2021/2/3 14:44
 */
@Erupt(name = "车次",
        primaryKeyCol = "trips_id",
        filter = [Filter("trips_delete_flag = 0")],
        dataProxy = [TripsDataProxy::class]
)
@SQLDelete(sql = "update trips set trips_delete_flag = 1 where trips_id = ?")
@Table(name = "trips")
@Entity
@DynamicUpdate
@DynamicInsert
class Trips : Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @EruptField(views = [View(title = "车次编号")])
    @Column(name = "trips_id")
    var tripsId: Int = -1

    @ManyToOne
    @EruptField(
            views = [View(title = "线路编号", column = "line_id")],
            edit = Edit(title = "线路选择", type = EditType.REFERENCE_TABLE, search = Search(),
                    referenceTableType = ReferenceTableType(label = "line_id", id = "line_id"))
    )
    @JoinColumn(name = "trips_line_id")
    var line: Line? = null

    @EruptField(
            views = [View(title = "列车名称", sortable = true)],
            edit = Edit(search = Search(), title = "列车名称", notNull = true, type = EditType.CHOICE, desc = "获取已有的列车",
                    choiceType = ChoiceType(
                            fetchHandler = arrayOf(SqlChoiceFetchHandler::class),
                            fetchHandlerParams = arrayOf("select train_name from train")
                    )
            )
    )
    @Column(name = "trips_train_name")
    var tripsTrainName: String = ""

    @EruptField(
            views = [View(title = "出发时间", sortable = true)],
            edit = Edit(title = "出发时间", type = EditType.DATE, notNull = true, search = Search(vague = true),
                    dateType = DateType(type = DateType.Type.DATE_TIME, pickerMode = DateType.PickerMode.FUTURE)
            )
    )
    @Column(name = "trips_start_time")
    var tripsStartTime: Date = Date()

    @EruptField(
            views = [View(title = "到达时间", sortable = true)],
            edit = Edit(title = "到达时间", type = EditType.DATE, notNull = true, search = Search(vague = true),
                    dateType = DateType(type = DateType.Type.DATE_TIME, pickerMode = DateType.PickerMode.FUTURE))
    )
    @Column(name = "trips_end_time")
    var tripsEndTime: Date = Date()

    @EruptField(
            views = [View(title = "一等座剩余座位数量", sortable = true)],
            edit = Edit(title = "一等座剩余座位数量", type = EditType.NUMBER, notNull = true,
                    search = Search(vague = true),
                    numberType = NumberType()
            )
    )
    @Column(name = "trips_first_seat_num")
    var tripsFirstSeatNum: Int = 0

    @EruptField(
            views = [View(title = "二等座剩余座位数量", sortable = true)],
            edit = Edit(title = "二等座剩余座位数量", type = EditType.NUMBER, notNull = true, search = Search(vague = true),
                    numberType = NumberType()
            )
    )
    @Column(name = "trips_second_seat_num")
    var tripsSecondSeatNum: Int = 0

    @EruptField(
            views = [View(title = "一等座票价", sortable = true)],
            edit = Edit(title = "一等座票价", type = EditType.NUMBER, notNull = true, search = Search(vague = true),
                    numberType = NumberType()
            )
    )
    @Column(name = "trips_first_seat_price")
    var tripsFirstSeatPrice: Float = 0f

    @EruptField(
            views = [View(title = "二等座票价", sortable = true)],
            edit = Edit(title = "二等座票价", type = EditType.NUMBER, notNull = true, search = Search(vague = true),
                    numberType = NumberType()
            )
    )
    @Column(name = "trips_second_seat_price")
    var tripsSecondSeatPrice: Float = 0f

    @Column(name = "trips_delete_flag")
    var tripsDeleteFlag  : Int? = null//逻辑删除标志
}